import {
  ParamArr
} from './paramarr'
import Global from './global'
import {
  getWebJsonMessage,
  regWebJsonMessage
} from '@/api/http'

export default class CPushMessages {
  constructor(lpfError) {
    this.lstObject = []
    this.lpfError = lpfError
    this.NoResult = '{B86254DE-8CDA-497D-A185-76432D03409E}'
  }

  getMessagesStr = (check) => {
    const msgs = []
    for (let i = 0; i < this.lstObject.length; i++) {
      const obj = this.lstObject[i]
      let cls = false
      try {
        // cls = obj.isClosed()  //todo 此处需要重新设计
      } catch (err) {
        cls = true
      }
      if ((check !== false) && cls) {
        this.lstObject.splice(i, 1)
        continue
      }
      for (let j = 0; j < obj.messages.length; j++) {
        const msg = obj.messages[j]
        if (msgs.indexOf(msg) === -1) msgs.push(msg)
      }
    }
    msgs.sort()
    return msgs.join(',')
  }

  getMessages = () => {
    const _this = this
    const msg = this.getMessagesStr()
    if (Global.getToken() === null || msg === '') {
      setTimeout(function() {
        _this.getMessages()
      }, 200)
      return
    }

    const vPar = new ParamArr()
    vPar.setStr('Msgs', msg)
    getWebJsonMessage(vPar).then(vPar => {
      // 缓存的消息
      if (vPar.getBool('Messages')) {
        for (let i = 0; i < vPar.getInt('Count'); i++) {
          const vTmp = new ParamArr(Global.parseWebJson(vPar.getStr(i)))
          window.top.mainMessages.postMessageToObjects(vTmp)
        }
      } else {
        window.top.mainMessages.postMessageToObjects(vPar)
      }
      window.top.mainMessages.getMessages()
    }).catch(({ sError, vPar }) => {
      if (!Global.SameText(sError, this.NoResult) && sError !== '流程已经加入队列') {
        if (window.top.mainMessages.lpfError !== undefined) {
          window.top.mainMessages.lpfError(vPar, sError)
        }
        window.top.mainMessages.getMessages()
        // 错误的时候返回
        window.top.mainMessages.postMessageToObjects(vPar)
      }
    })
  }

  /**
   * 注册信息
   */
  regMessages = (obj, messages, isReg) => {
    if (obj === undefined || messages === undefined || messages.length === 0) return
    const msg1 = this.getMessagesStr()

    if (obj.messages === undefined) obj.messages = []
    for (let i = 0; i < messages.length; i++) {
      const msg = messages[i]
      if (obj.messages.indexOf(msg) === -1) obj.messages.push(msg)
    }
    if (this.lstObject.indexOf(obj) === -1) this.lstObject.push(obj)

    // 比较信息
    const msg2 = this.getMessagesStr()
    if (msg1 !== msg2) {
      const vPar = new ParamArr()
      vPar.setStr('Msgs', msg2)
      regWebJsonMessage(vPar)
    }
  }

  delMessages = (obj, messages) => {
    const index = this.lstObject.indexOf(obj)
    if (index === -1 || obj.messages === undefined) return

    const msg1 = this.getMessagesStr()

    if (messages === undefined || messages.length === 0) {
      obj.messages = undefined
    } else {
      for (let i = 0; i < messages.length; i++) {
        const j = obj.messages.indexOf(messages[i])
        if (j !== -1) obj.messages.splice(j, 1)
      }
    }
    if (obj.messages === undefined || obj.messages.length === 0) {
      this.lstObject.splice(index, 1)
    }

    // 比较信息
    const msg2 = this.getMessagesStr()
    if (msg1 === msg2) return

    const vPar = new ParamArr()
    vPar.setStr('Msgs', msg2)
    regWebJsonMessage(vPar)
  }

  postMessageToObjects = (vPar, againReg) => {
    let bReg = false
    let msg1 = ''

    const msg = vPar.getMsgId()
    for (let i = 0; i < this.lstObject.length; i++) {
      const obj = this.lstObject[i]
      let cls = false
      try {
        // cls = obj.isClosed()  todo此处需要重新写
      } catch (err) {
        cls = true
      }
      if (cls) {
        if (bReg === false) msg1 = this.getMessagesStr(false)
        this.lstObject.splice(i, 1)
        bReg = true
        continue
      }
      if (obj.messages.indexOf(msg) !== -1 && obj.WndProc !== undefined) {
        // 解决因消息推送到页面，任何一个错误导致整个消息推送系统瘫痪的问题
        try {
          obj.WndProc(msg, vPar)
        } catch (err) {
          console.log(err)
        }
      }
    }

    if (bReg && (againReg === undefined || againReg !== false)) {
      const msg2 = this.getMessagesStr()
      if (msg1 !== msg2) {
        const vPar = new ParamArr()
        vPar.setStr('Msgs', msg2)
      }
    }
  }
}
